package bl.controller;

import java.util.ArrayList;
import java.util.Iterator;

import po.CatalogPO;

import database.factory.CatalogDataFactory;
import database.factory.CatalogDataFactoryImpl;
import database.service.CatalogDataService;
import vo.CatalogVO;
import vo.BLToViewResultMessage;
import bl.domain.CatalogManagement;
import bl.service.CatalogManagementBLService;

public class CatalogManagementController implements CatalogManagementBLService {
	
	private CatalogManagement catalogManagement;
	
	public CatalogManagementController() {
		catalogManagement = new CatalogManagement();
	}
	
	private void init() {
		CatalogDataFactory catalogDataFactory = new CatalogDataFactoryImpl();
		CatalogDataService catalogDataService = catalogDataFactory.getCatalogDataService();
		catalogDataService.load();
		catalogManagement.setCatalogItr(catalogDataService.getIterator());
	}

	@Override
	public ArrayList<CatalogVO> getCatalogList() {
		init();
		Iterator<CatalogPO> catalogItr = catalogManagement.getCatalogItr();
		ArrayList<CatalogVO> catalogVOs = new ArrayList<CatalogVO>();
		while(catalogItr.hasNext()) {
			CatalogPO cpo = catalogItr.next();
			if (cpo.getID() > 0) {
				catalogVOs.add(new CatalogVO(cpo));
			}
		}
		return catalogVOs;
	}

	@Override
	public BLToViewResultMessage addCatalog(String catalogName) {
		if (catalogName.equals("")) {
			return null;
		}
		
		init();
		CatalogPO newCatalog = new CatalogPO();
		newCatalog.setName(catalogName);
		
		CatalogDataFactory catalogDataFactory = new CatalogDataFactoryImpl();
		CatalogDataService catalogDataService = catalogDataFactory.getCatalogDataService();
		catalogDataService.load();
		int newCatalogID = catalogDataService.insert(newCatalog);
		catalogDataService.save();
		
		if (newCatalogID > 0) {
			return BLToViewResultMessage.ADD_CATALOG_SUCCESS;
		} else {
			return null;
		}
	}

	@Override
	public BLToViewResultMessage deleteCatalog(int catalogID) {
		if (catalogID == 0) {
			return null;
		}
		
		init();
		CatalogDataFactory catalogDataFactory = new CatalogDataFactoryImpl();
		CatalogDataService catalogDataService = catalogDataFactory.getCatalogDataService();
		catalogDataService.load();
		CatalogPO catalogPO = catalogDataService.find(catalogID);
		if (catalogPO == null) {
			return null;
		}
		catalogDataService.delete(catalogPO);
		catalogDataService.save();
		
		return BLToViewResultMessage.DELETE_CATALOG_SUCCESS;
	}

	@Override
	public BLToViewResultMessage modifyCatalog(int catalogID, String catalogName) {
		if (catalogID == 0 || catalogName == "") {
			return null;
		}
		
		init();
		CatalogPO newCatalog = new CatalogPO();
		newCatalog.setName(catalogName);
		
		CatalogDataFactory catalogDataFactory = new CatalogDataFactoryImpl();
		CatalogDataService catalogDataService = catalogDataFactory.getCatalogDataService();
		catalogDataService.load();
		CatalogPO catalogPO = catalogDataService.find(catalogID);
		if (catalogPO == null) {
			return null;
		}
		newCatalog.setId(catalogPO.getID());
		catalogDataService.update(newCatalog);
		catalogDataService.save();
		
		return BLToViewResultMessage.MODIFY_CATALOG_SUCCESS;
	}

}
